import { ref } from 'vue'
import { defineStore } from 'pinia'
export const useUserStore = defineStore(
  'user',
  () => {
    const token = ref('')
    let userInfo = ref({})
    let menus = ref([])
    const routesList = ref([])
    const setMenus = (newMenus) => {
      menus.value = newMenus
    }
    const setRoutesList = (newRoutes) => {
      routesList.value = newRoutes
    }
    const getRoutesList = () => {
      return routesList.value
    }
    const removeRoutesList = () => {
      routesList.value = []
    }
    const getMenus = () => {
      return menus.value
    }
    const removeMenus = () => {
      menus.value = []
    }
    const setToken = async (newToken) => {
      token.value = newToken
    }
    const setUserInfo = (newUserInfo) => {
      userInfo.value = newUserInfo
    }
    const getUserInfo = () => {
      return userInfo.value
    }
    const removeUserInfo = () => {
      userInfo.value = {}
    }
    const getToken = () => {
      return token.value
    }
    const removeToken = () => {
      token.value = ''
    }
    const logout = () => {
      removeToken()
      removeUserInfo()
      removeRoutesList()
      removeMenus()
    }

    return {
      token,
      menus,
      userInfo,
      routesList,
      setToken,
      getToken,
      getRoutesList,
      setRoutesList,
      removeUserInfo,
      removeToken,
      removeRoutesList,
      removeMenus,
      setUserInfo,
      getUserInfo,
      setMenus,
      getMenus,
      logout
    }
  },
  {
    persist: {
      storage: sessionStorage // 存储方式
    }
  }
)
export default useUserStore
